package com.lykj.llw.box.ui.activity

import android.graphics.Typeface
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import com.blankj.utilcode.util.ColorUtils
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.lykj.lib_base.adapter.viewpager.FragmentSPAdapter
import com.lykj.lib_base.base.BaseActivity
import com.lykj.lib_base.internal.extfun.clickWithTrigger
import com.lykj.llw.box.R
import com.lykj.llw.box.databinding.ActMyCouponBinding
import com.lykj.llw.box.ui.fragment.ReUseCouponFragment

/**
 * ================================================
 * 我的优惠券
 * ================================================
 *
 * @author gosha
 * @date 2023/10/17-11:12
 */
class MyCouponActivity : BaseActivity() {
    lateinit var binding: ActMyCouponBinding

    private val mFragments = ArrayList<Fragment>()
    private val mUnUseCouponFragment = ReUseCouponFragment()
    private val mUsedCouponFragment = ReUseCouponFragment()
    private val mExpiredCouponFragment = ReUseCouponFragment()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActMyCouponBinding.inflate(layoutInflater)
        setContentView(binding.root)
        initUI()
        initData()
        setListener()
    }

    override fun onResume() {
        super.onResume()

        ImmersionBar.with(this)
            .statusBarDarkFont(true) //状态栏字体是深色，不写默认为亮色
            .navigationBarDarkIcon(true)
            .hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR)
            .navigationBarEnable(false)
            .init()
    }

    override fun initUI() {
        binding.titleBar.tvTitle.text = "我的优惠券"
        binding.titleBar.tvRightOption.visibility = View.INVISIBLE

        val mUnUseBundle = Bundle()
        mUnUseBundle.putInt("type", 0)

        val mUsedBundle = Bundle()
        mUsedBundle.putInt("type", 1)

        val mExpiredBundle = Bundle()
        mExpiredBundle.putInt("type", 2)

        mUnUseCouponFragment.arguments = mUnUseBundle
        mUsedCouponFragment.arguments = mUsedBundle
        mExpiredCouponFragment.arguments = mExpiredBundle

        mFragments.add(mUnUseCouponFragment)
        mFragments.add(mUsedCouponFragment)
        mFragments.add(mExpiredCouponFragment)

        //设置viewpager
        val adapter = FragmentSPAdapter(supportFragmentManager, mFragments)
        binding.vpCoupon.offscreenPageLimit = 2
        binding.vpCoupon.adapter = adapter
        switchNav(0)
    }

    override fun initData() {
    }

    //重置底部导航栏状态
    private fun restNav() {
        binding.tvBtnUnuse.setTextColor(ColorUtils.getColor(R.color.grey_FF787878))
        binding.tvBtnUsed.setTextColor(ColorUtils.getColor(R.color.grey_FF787878))
        binding.tvBtnExpired.setTextColor(ColorUtils.getColor(R.color.grey_FF787878))

        binding.tvBtnUnuse.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
        binding.tvBtnUsed.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
        binding.tvBtnExpired.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)

        binding.tvBtnUnuse.setBackgroundResource(R.drawable.shape_half_fff7f7f7)
        binding.tvBtnUsed.setBackgroundResource(R.drawable.shape_half_fff7f7f7)
        binding.tvBtnExpired.setBackgroundResource(R.drawable.shape_half_fff7f7f7)
    }

    //切换到导航栏到指定页面
    private fun switchNav(position: Int) {
        restNav()
        when (position) {
            0 -> {
                binding.tvBtnUnuse.setTextColor(ColorUtils.getColor(R.color.blue_FF664FC3))
                binding.tvBtnUnuse.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
                binding.tvBtnUnuse.setBackgroundResource(R.drawable.shape_half_ffe7e0ff)
            }

            1 -> {
                binding.tvBtnUsed.setTextColor(ColorUtils.getColor(R.color.blue_FF664FC3))
                binding.tvBtnUsed.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
                binding.tvBtnUsed.setBackgroundResource(R.drawable.shape_half_ffe7e0ff)
            }

            2 -> {
                binding.tvBtnExpired.setTextColor(ColorUtils.getColor(R.color.blue_FF664FC3))
                binding.tvBtnExpired.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
                binding.tvBtnExpired.setBackgroundResource(R.drawable.shape_half_ffe7e0ff)
            }
        }
        binding.vpCoupon.setCurrentItem(position, false)
    }

    override fun setListener() {
        binding.titleBar.ivBack.clickWithTrigger {
            finish()
        }

        binding.tvBtnUnuse.clickWithTrigger {
            switchNav(0)
        }

        binding.tvBtnUsed.clickWithTrigger {
            switchNav(1)
        }

        binding.tvBtnExpired.clickWithTrigger {
            switchNav(2)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
    }
}